Page({
  data: {
    passport: {},
  },
  onLoad({ id, encryptedData }) {
    if (encryptedData) {
      this.getSharePassport(encryptedData)
    } else {
      this.getPassport(id)
    }
  },
  // 获取通行证
  async getPassport(id) {
    const res = await wx.http.get(`/visitor/${id}`)
    this.setData({
      passport: res.data,
    })
  },
  // 获取转发通行证
  async getSharePassport(encryptedData) {
    const res = await wx.http.get(`/visitor/share/${encryptedData}`)
    this.setData({
      passport: res.data,
    })
  },
  onShareAppMessage() {
    const { encryptedData } = this.data.passport
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?encryptedData=' + encryptedData,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },
  async savePassport() {
    const { path } = await wx.getImageInfo({ src: this.data.passport.url })
    wx.saveImageToPhotosAlbum({ filePath: path })
  },
})
